home *** CD-ROM | disk | FTP | other *** search
/ Info-Mac 1992 August / info-mac-1992.iso / UNIX / Skim Digest.txt < prev    next >
Internet Message Format  |  1992-08-29  |  6KB

  1. Date: Thu, 28 Feb 91 03:00:17 CST
  2. From: 231b3679@fergvax.unl.edu (Mike Gleason)
  3. Subject: [*] skim-digest.c
  4.  
  5.  
  6. /* skim-digest.c 
  7.  * version 1.0, 28 Feb 91, by Mike Gleason, NCEMRSoft.
  8.  * 
  9.  * Purpose: To read only selected articles from the Info-Mac digests.
  10.  *
  11.  * How to use: Compile this C source code with your favorite C compiler
  12.  *   on your favorite unix host:
  13.  * 
  14.  *   % cc skim-digest.c -O -o skim
  15.  * 
  16.  *   Go out and download a digest from sumex-aim.stanford.edu.  Run skim:
  17.  * 
  18.  *   % skim digest50 digest51 digest49
  19.  *
  20.  *   It should be fairly straightforward from there.  When skim finds an
  21.  *   article in the digest, it will ask you if you want to read it.  Type
  22.  *   <n> <return> to skip the article, or just hit <return> to go ahead
  23.  *   and read it.  Skim's key commands are similar to the "less" and
  24.  *   "more" text readers; the biggest difference is that Skim only
  25.  *   recognizes a few keys (in other words, fancy-shmancy stuff like
  26.  *   grepping through the digest is not supported [yet]).  Once you choose
  27.  *   to read an article, it will pause every screenfull of lines.  Hit
  28.  *   <n> <return> to go to the next article, <q> <return> to quit reading
  29.  *   the current digest, or just hit <return> to continue reading.
  30.  *
  31.  * Lastly: I'm curious if anyone else out there besides me will use this;
  32.  *   If you do, drop me a line at 231b3679@fergvax.unl.edu.  I'm putting
  33.  *   this out into the public domain, so go ahead and submit any revisions
  34.  *   to sumex.
  35.  */
  36.  
  37.  
  38.  
  39. #define NO_HOT_KEYS
  40.  
  41. #define SCREEN_HEIGHT 24
  42. #define AT_NEW 1
  43. #define QUIT -1
  44. #define NEXT 0
  45. #define NOT_AT_NEW 0
  46. #define CLEARSCREEN printf("\033[2J \033[H")   /* Clear screen. */
  47. #ifdef THINK_C
  48. #  define NO_ANSI
  49. #  include "stdio.h"
  50. #else
  51. #  include <stdio.h>
  52. /* #  define NO_ANSI */  
  53.    /* Uncomment the preceding line if your terminal doesn't like 
  54.       ANSI screen control characters. */
  55. #endif
  56.  
  57. int      ReadDigest();
  58. int      ReadArticle();
  59. char     Response();
  60. int      StrnEqual();
  61.  
  62. int   main(argc, argv)
  63.    int argc;
  64.    char **argv;
  65. {
  66.    char digestName[80];
  67.    short i;
  68.    
  69.    if (argc < 2)
  70.    {
  71.       printf("  digest to read: ");
  72.       fgets(digestName, sizeof (digestName), stdin);
  73.       *(digestName + strlen(digestName) - 1) = '\0';
  74.       if (digestName)
  75.          ReadDigest(digestName);
  76.    }
  77.    else
  78.    {
  79.       for (i=1; i<argc; i++)
  80.          ReadDigest(argv[i]);
  81.    }
  82.  
  83. #ifdef NO_ANSI
  84.          printf("\n(End)\n\n");
  85. #else
  86.          printf("\n\033[1m(End)\033[0m\n\n");
  87. #endif
  88. }
  89.  
  90.  
  91.  
  92.  
  93. int      ReadDigest(digestName)
  94.    char *digestName;
  95. {
  96.    FILE  *in;
  97.    char  lyne[128], dateStr[128];
  98.    short articleNum = 0, atNextArticle = NOT_AT_NEW;
  99.    
  100.    if (!(in = fopen(digestName, "r")))
  101.    {
  102.       fprintf(stderr, "skim-digest: \"%s\" could not be opened.\n",
  103.          digestName);
  104.       return (0);
  105.    }
  106. #ifdef NO_ANSI
  107.          printf("Digest: %s:\n\n", digestName);
  108. #else
  109.          CLEARSCREEN;
  110.          printf("\033[7mDigest: \"%s\"\033[0m \n\n", digestName);
  111. #endif
  112.    
  113.    while (1)
  114.    {  /* Go through the digest line by line, looking for "Date:" */
  115.       if (atNextArticle != AT_NEW)
  116.       {
  117.          if (!fgets(lyne, sizeof(lyne), in))
  118.              break;
  119.          if (!StrnEqual(lyne, "Date:", 5))
  120.             continue;
  121.          strcpy(dateStr, lyne);
  122.       }
  123.       articleNum++;
  124.       atNextArticle = NOT_AT_NEW;
  125. #ifdef NO_ANSI
  126.       printf("Article #%d:\n%s", articleNum, dateStr);
  127. #else
  128.       if (articleNum > 1) CLEARSCREEN;
  129.       printf("\033[1mArticle #%d\033[0m:\n%s", articleNum, dateStr);
  130. #endif
  131.       while (fgets(lyne, sizeof(lyne), in))
  132.       {
  133.          if (strlen(lyne) < 5)
  134.             break;
  135.          fputs(lyne, stdout);
  136.          /* Write all the header lines. */
  137.       }
  138.       if (!lyne)
  139.       {
  140.          fclose(in);
  141.          return (0);  /* unexpected eof */
  142.       }
  143.       
  144.       *lyne = Response("\nRead this post? ");
  145.       
  146.       switch (*lyne)
  147.       {
  148.          case 'n': case 'N': break;
  149.          case 'q': case 'Q': return (1); break;
  150.          default: 
  151.          {
  152.             if ((atNextArticle = ReadArticle(in, dateStr)) < 0)
  153.             {
  154.                fclose(in);
  155.                return (1);
  156.             }
  157.          }
  158.       }
  159.    }
  160.    
  161.    fclose(in);
  162.    return (1);
  163. }  /* ReadDigest */
  164.  
  165.  
  166.  
  167.  
  168.  
  169. int      ReadArticle(in, dateStr)
  170.    FILE *in;
  171.    char *dateStr;
  172. {
  173.    char  lyne[128];
  174.    register short i;
  175.    
  176.    while(1)
  177.    {
  178. #ifndef NO_ANSI
  179.       CLEARSCREEN;
  180. #endif
  181.       for (i=0; i<SCREEN_HEIGHT-1; i++)
  182.       {
  183.          if (!fgets(lyne, sizeof(lyne), in))
  184.             return (QUIT); /* unexpected eof */
  185.          if (StrnEqual(lyne, "Date:", 5))
  186.          {
  187.             strcpy(dateStr, lyne);
  188. #ifndef NO_ANSI
  189.             *lyne = Response("(Hit Return)");
  190. #endif
  191.             return (AT_NEW);  /* end of article, at beginning of next. */
  192.          }
  193.          fputs(lyne, stdout);
  194.       }
  195.             
  196.       *lyne = Response(":");
  197.       switch (*lyne)
  198.       {
  199.          case 'n': case 'N': return (NEXT);  /* screw this article... */
  200.          case 'q': case 'Q': return (QUIT); break;
  201.       }
  202.    }
  203. }  /* ReadArticle */
  204.  
  205.  
  206.  
  207.  
  208. char  Response(prompt)
  209.    char *prompt;
  210. {
  211. #if defined(MAC) || defined(NO_HOT_KEYS) 
  212.    char lyne[128];
  213. #endif
  214.    
  215. #ifndef NO_ANSI
  216.    printf("\033[7m%s\033[0m", prompt);
  217. #else
  218.    printf("%s", prompt);
  219. #endif
  220.  
  221. #if defined(MAC) || defined(NO_HOT_KEYS) 
  222.    fgets(lyne, sizeof(lyne), stdin);
  223.    return (*lyne);
  224. #else
  225.    return (fgetc(stdin));
  226. #endif
  227. }  /* Response */
  228.  
  229.  
  230.  
  231.  
  232. int    StrnEqual(a, b, n)
  233.    char *a, *b;
  234.    int n;
  235. {
  236.     register char *s, *t;
  237.     register int i;
  238.     
  239.     s = a; t = b;
  240.     for (i=0; i<n; i++)
  241.     {
  242.         if (*s != *t) return (0);
  243.         if (*s == '\0' || *t == '\0') return (1);
  244.         s++; t++;
  245.     }
  246.     return (1);
  247. }   /* StrnEqual */
  248.  
  249. /* eof */
  250.  
  251.